<template>
  <div class="calendar-container">
    <!-- 控制栏 -->
    <div class="controls">
      <button @click="prevMonth">&lt;</button>
      <select v-model="selectedYear" @change="updateDays">
        <option v-for="y in years" :key="y" :value="y">{{ y }}</option>
      </select>
      <select v-model="selectedMonth" @change="updateDays">
        <option v-for="(m, idx) in months" :key="idx" :value="idx">{{ m }}</option>
      </select>
      <button @click="nextMonth">&gt;</button>
    </div>

    <!-- 日历网格 -->
    <div class="month-calendar">
      <!-- 星期标题 -->
      <div v-for="(day, index) in weekDays" :key="index" class="day-header">{{ day }}</div>
      <!-- 日期格子 -->
      <div v-for="(day, index) in days" :key="index" class="day-cell">
        <div class="day" :class="getDayClass(day)" :title="getTooltip(day)">
        </div>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import getLunarDate, {getLunarDayInChinese} from "@/utils/getLunarDate.ts";

const selectedYear = ref(2025)
const selectedMonth = ref(5) // 从 0 开始，6 表示 6 月

// 示例数据：模拟某个月的贡献记录
const contributions = ref({
  '2025-06-01': 2,
  '2025-06-02': 0,
  '2025-06-03': 4,
  '2025-06-05': 1,
  '2025-06-10': 3,
  '2025-06-15': 5,
})

// ====== 静态数据 ======
const months = [
  '一月', '二月', '三月', '四月',
  '五月', '六月', '七月', '八月',
  '九月', '十月', '十一月', '十二月'
]

const weekDays = ['日', '一', '二', '三', '四', '五', '六']

const years = Array.from({length: 10}, (_, i) => 2021 + i)

// ====== 计算属性：生成当前月份的每一天 ======
const days = computed(() => {
  const year = selectedYear.value
  const month = selectedMonth.value

  const firstDay = new Date(year, month, 1)
  const lastDay = new Date(year, month + 1, 0)

  const result = []

  let currentDate = new Date(year, month, 1)

  console.log("currentDate", firstDay.getDay())
  // 填充空白到第一个星期一是第一列（GitHub 风格）
  if (firstDay.getDay() !== 0) {
    for (let i = 0; i < firstDay.getDay(); i++) {
      result.unshift(null)
    }
  }
  for (let d = 1; d <= lastDay.getDate(); d++) {
    const dateStr = `${year}-${String(month + 1).padStart(2, '0')}-${String(d).padStart(2, '0')}`
    console.log("year", year, "month", month + 1, "d", d)
    result.push({
      date: dateStr,
      count: contributions.value[dateStr] || 0,
      day: new Date(dateStr).getDay(),
      // 天数编号
      dayNumber: d,
      // 阴历
      lunarDayChinese: getLunarDayInChinese(year, month + 1, d)
    })
  }

  console.log("res", result)
  return result
})

function getDayClass(day) {
  if (!day) return ''
  const count = day.count
  if (count === 0) return 'level-0'
  if (count < 2) return 'level-1'
  if (count < 5) return 'level-2'
  if (count < 10) return 'level-3'
  return 'level-4'
}

function getTooltip(day) {
  if (!day) return ''
  return `${day.date} 贡献了 ${day.count} 次`
}

function updateDays() {
  // 可以在这里加载新的 contribution 数据
}

function prevMonth() {
  if (selectedMonth.value === 0) {
    selectedMonth.value = 11
    selectedYear.value--
  } else {
    selectedMonth.value--
  }
  updateDays()
}

function nextMonth() {
  if (selectedMonth.value === 11) {
    selectedMonth.value = 0
    selectedYear.value++
  } else {
    selectedMonth.value++
  }
  updateDays()
}
</script>

<style scoped lang="scss">
.calendar-container {
  width: 100%;
  font-family: sans-serif;
  margin: 16px auto;
  padding: 16px;
  background-color: var(--blog-background-card-color);
}

.controls {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 10px;
  margin-bottom: 10px;
}

.month-calendar {
  display: grid;
  grid-template-columns: repeat(7, 1fr);
  gap: 4px;
}

.day-header {
  text-align: center;
  font-weight: bold;
  font-size: 12px;
  color: black;
}

.day-cell {
  display: flex;
  justify-content: center;
  align-items: center;
}

.day {
  width: 32px;
  height: 32px;
  border-radius: 4px;
  transition: background-color 0.2s ease;
}

.level-0 {
  background-color: #ebedf0;
}

.level-1 {
  background-color: #c6e48b;
}

.level-2 {
  background-color: #7bc96f;
}

.level-3 {
  background-color: #239a3b;
}

.level-4 {
  background-color: #196127;
}
</style>
